#include <string>  
#include <sstream>  
#include <iostream>
#include "sis3100_vme_calls.h"
#include "IOError.h"  
#include "vme_sis.h"

using namespace std;  

#define SIS3100_VERSION 0x0

/*---------------------------------------------------------------------------*/
void show_SIS3100_status(int handle) throw(IOError)
/*---------------------------------------------------------------------------*/
{
		u_int32_t d;
		int r;

		cerr << "SIS3100 status:";
		if((r=s3100_control_read(handle,SIS3100_VERSION,&d))) {
				stringstream ss;

				ss << "show_SIS3100_status():\n"
					 << "  s3100_control_read() returned " << r << "\n";

				throw IOError(ss.str().c_str(),r);
		}
		cerr << " FW Version: " << ((d & 0xff000000)>>24)
				 << " FW ID: " << ((d & 0x00ff0000)>>16)
				 << " HW Version: " << ((d & 0x0000ff00)>>8)
				 << " Identifier: " << (d & 0x000000ff) << endl;
}
/*---------------------------------------------------------------------------*/


void readA16D32(int handle, u_int16_t addr, u_int32_t *dest) throw(IOError)
{
	int r;

	r = vme_A16D32_read(handle, addr, dest);
	if (r != 0) {
		stringstream ss;
		
		ss << hex << "readA16D32(): "
			 << "vme_A16D32_read() returned non zero"
			 << ", handle=0x" << handle
			 << ", address=0x" << addr
			 << ", data_address=0x" << dest
			 << dec;
		throw IOError(ss.str().c_str(),r);
	}
}


void readA32D16(int handle, u_int32_t addr, u_int16_t *dest) throw(IOError)
{
	int r;

	r = vme_A32D16_read(handle, addr, dest);
	if (r != 0) {
		stringstream ss;
		
		ss << hex << "readA32D16(): "
			 << "vme_A32D16_read() returned non zero"
			 << ", handle=0x" << handle
			 << ", address=0x" << addr
			 << ", data_address=0x" << dest
			 << dec;
		throw IOError(ss.str().c_str(),r);
	}
}


void readA32D32(int handle, u_int32_t addr, u_int32_t *dest) throw(IOError)
{
	int r;

	r = vme_A32D32_read(handle, addr, dest);
	if (r != 0) {
			stringstream ss;
			
		ss << hex << "readA16D32(): "
			 << "vme_A32D32_read() returned non zero"
			 << ", handle=0x" << handle
			 << ", address=0x" << addr
			 << ", data_address=0x" << dest
			 << dec;
		throw IOError(ss.str().c_str(),r);
	}
}


void readA16D32DMA(int handle, u_int16_t addr, u_int32_t *buf, u_int32_t count) throw(IOError)
{
	stringstream ss;
	u_int32_t read;
	int r;
	
	r = vme_A16DMA_D32FIFO_read(handle, addr, buf, count, &read);
    if (r != 0) {
		ss << hex
			 << "readA16D32DMA():"
			 << " vme_A16DMA_D32FIFO_read() returned non zero"
			 << ",  parameters: handle=0x" << handle
			 << ", vme_address=0x" << addr
			 << ", buffer_adress=0x" << (u_int32_t)buf
			 << ", count=0x" << count
			 << ", read=0x" << read
			 << dec << endl;
		throw IOError(ss.str().c_str(),r);
	}

	if (read!=count) {
		ss << "readA16D32DMA():"
			 << " vme_A16DMA_D32FIFO_read() returned " << read
			 << "bytes - requested were " << count 
			 << endl;
		
		throw IOError(ss.str().c_str(), r);
	}
}


void readA32D32DMA(int handle, u_int32_t addr, u_int32_t *buf, u_int32_t count) throw(IOError)
{
	stringstream ss;
	u_int32_t read;
	int r;

	r = vme_A32DMA_D32FIFO_read(handle, addr, buf, count, &read);
    if (r != 0) {
		ss << hex
			 << "readA32D32DMA():"
			 << " vme_A32DMA_D32FIFO_read() returned non zero"
			 << ",  parameters: handle=0x" << handle
			 << ", vme_address=0x" << addr
			 << ", buffer_adress=0x" << (u_int32_t)buf
			 << ", count=0x" << count
			 << ", read=0x" << read
			 << dec << endl;
		throw IOError(ss.str().c_str(), r);
	}

	if (read != count) {
		ss << "readA32D32DMA():"
			 << " vme_A32DMA_D32FIFO_read() returned " << read
			 << "bytes - requested were " << count 
			 << endl;
		throw IOError(ss.str().c_str(), r);
	}
}


void writeA32D16(int handle,u_int32_t addr,u_int16_t d) throw(IOError)
{
    int r;
    
	r = vme_A32D16_write(handle, addr, d);
    if (r != 0) {
		stringstream ss;

		ss << hex
			 << "writeA32D16(): vme_A32D16_write() returned non zero"
			 << ",  parameters: handle=0x" << handle
			 << ", vme_address=0x" << addr
			 << ", data=0x" << d
			 << dec << endl;
		throw IOError(ss.str().c_str(), r);
	}
}
